﻿// FrequencyModulationControl.xaml.cs (c) Copyright 2009, Mike Hodnick : www.hodnick.com

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Kindohm.KSynth.Library;

namespace Kindohm.KSynth.AudioCanvas
{
    public partial class FrequencyModulationControl : UserControl, ICanvasControl
    {
        bool envelopeIsVisible;
        Color oldColor;
        Oscillator oscillator;
        ObservableCollection<WaveForm> waveForms;

        public FrequencyModulationControl()
        {
            InitializeComponent();
            this.waveForms = new ObservableCollection<WaveForm>(){
                 WaveForm.Sine, WaveForm.Saw, WaveForm.Square, WaveForm.Triangle};
            this.waveFormList.ItemsSource = this.waveForms;
            this.waveFormList.SelectedIndex = 0;
            this.Loaded += new RoutedEventHandler(FrequencyModulationControl_Loaded);
        }

        public bool EnvelopeIsVisible
        {
            get { return this.envelopeIsVisible; }
            set { this.envelopeIsVisible = value; }
        }

        public Oscillator Oscillator
        {
            get { return this.oscillator; }
            set
            {
                this.oscillator = value;
                this.oscillator.FrequencyModulate(
                    this.frequencyUpDown, NumericUpDown.ValueProperty,
                    this.amplitudeUpDown, NumericUpDown.ValueProperty);
                this.oscillator.FrequencyModulationWaveForm(
                    this.waveFormList, ComboBox.SelectedItemProperty);
            }
        }

        void FrequencyModulationControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.amplitudeUpDown.Maximum = uint.MaxValue;
            this.MouseEnter += new MouseEventHandler(FrequencyModulationControl_MouseEnter);
            this.MouseLeave += new MouseEventHandler(FrequencyModulationControl_MouseLeave);
        }

        void FrequencyModulationControl_MouseLeave(object sender, MouseEventArgs e)
        {
            this.HideMenu();
        }

        void FrequencyModulationControl_MouseEnter(object sender, MouseEventArgs e)
        {
            this.ShowMenu();
        }

        public TranslateTransform Translation
        {
            get { return this.translation; }
        }

        private void Path_MouseEnter(object sender, MouseEventArgs e)
        {
            Path path = sender as Path;
            SolidColorBrush brush = path.Fill as SolidColorBrush;
            this.oldColor = brush.Color;
            brush.Color = Color.FromArgb(255, 66, 66, 66);
        }

        private void Path_MouseLeave(object sender, MouseEventArgs e)
        {
            Path path = sender as Path;
            SolidColorBrush brush = path.Fill as SolidColorBrush;
            brush.Color = oldColor;
        }

        private void Path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.envelopeIsVisible)
            {
                this.envelopeIsVisible = false;
                if (this.HideEnvelopeButtonClicked != null)
                    this.HideEnvelopeButtonClicked(this, EventArgs.Empty);
            }
            else
            {
                this.envelopeIsVisible = true;
                if (this.ShowEnvelopeButtonClicked != null)
                    this.ShowEnvelopeButtonClicked(this, EventArgs.Empty);
            }
        }

        void ShowMenu()
        {
            this.menuCanvas.Visibility = Visibility.Visible;
        }

        void HideMenu()
        {
            this.menuCanvas.Visibility = Visibility.Collapsed;
        }


        public event EventHandler ShowEnvelopeButtonClicked;
        public event EventHandler HideEnvelopeButtonClicked;

    }
}
